#!/usr/bin/env python
# -*- coding:utf-8 -*-

'''
    将编译好的文件安装到公共目录
    历史:
        姚彧 2017-02-12 创建
'''

import os
import shutil

def copy_files_in_dir(source_dir, dest_dir):
    assert(os.path.isdir(source_dir)), source_dir
    if not os.path.isdir(dest_dir):
        os.makedirs(dest_dir)
    for item in os.listdir(source_dir):
        src_name = os.path.join(source_dir, item)
        dest_name = os.path.join(dest_dir, item)
        if os.path.isfile(src_name):
            print('  %s -> %s' % (src_name, dest_name))
            shutil.copy2(src_name, dest_name)
        elif os.path.isdir(src_name):
            copy_files_in_dir(src_name, dest_name)

    

if __name__ == '__main__':
    import os
    def get_filename(rootdir):
        rootdir = os.path.abspath(rootdir)
        def __call__(name):
            return os.path.join(rootdir, name)
        return __call__
    get_src_name = get_filename(r'F:\Develop\GitRepos\OpenSceneGraph3rdParty\libiconv')
    get_dst_name = get_filename(r'F:\Develop\bin\x64\libiconv')
    
    publish_list = [('include/iconv.h', 'include'),
                    ('Debug_x64/libiconvd.dll', 'bin'),
                    ('Debug_x64/libiconvd.pdb', 'bin'),
                    ('Debug_x64/libiconvd.lib', 'lib'),
                    ('Release_x64/libiconv.dll', 'bin'),
                    ('Release_x64/libiconv.pdb', 'bin'),
                    ('Release_x64/libiconv.lib', 'lib'),
                    ]
    for _name in publish_list:
        if isinstance(_name, str):
            name, dest_name = _name, ''
        else:
            name, dest_name = _name
        src = get_src_name(name)
        dst = get_dst_name(dest_name)
        print('%s -> %s' % (src, dst))
        if os.path.isfile(src):
            shutil.copy2(src, dst)
        elif os.path.isdir(src):    
            copy_files_in_dir(src, dst)
        else:
            print('  --> 错误文件: %s' % src)
    print('文件发布完成')